




//
//  MineViewController.m
//  小碗旅行
//
//  Created by wyzcWLB on 16/9/26.
//  Copyright © 2016年 wyzcWLB. All rights reserved.
//

#import "MineViewController.h"
#import "UIImageView+WebCache.h"
#import "KVNProgress.h"
#import "xiaowanViewController.h"
#import "ConnectViewController.h"
#import "MapViewController.h"

#define Screen_Width [UIScreen mainScreen].bounds.size.width
#define Screen_Height [UIScreen mainScreen].bounds.size.height

@interface MineViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UIImageView *headerView;
@property(nonatomic,strong)UIImageView *headerImg;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSString *str;
@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatHeaderView];
    [self creatTableView];
}

-(void)creatHeaderView{
    
    self.headerView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height*0.25)];
    _headerView.image = [UIImage imageNamed:@"bg_guide"];
    _headerView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_headerView];
    
    self.headerImg = [[UIImageView alloc]initWithFrame:CGRectMake(Screen_Width*0.375,Screen_Height*0.05, Screen_Height*0.125,Screen_Height*0.125)];
    
    _headerImg.image = [UIImage imageNamed:@"iTunesArtwork"];
    [self.headerView addSubview:_headerImg];
    //剪成圆图
    self.headerImg.layer.cornerRadius = Screen_Height*0.125*0.5;
    self.headerImg.layer.masksToBounds = YES;
    
}
-(void)creatTableView{
    
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _tableView.frame = CGRectMake(0, Screen_Height*0.25, Screen_Width, Screen_Height-Screen_Height*0.25);
    //设置内边距
    [self.tableView setContentInset:UIEdgeInsetsMake(-30, 0, 0, 0)];
    
    [self.view addSubview:_tableView];
    //遵守数据源，代理
    _tableView.delegate = self;
    _tableView.dataSource = self;
}
//设置两组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
//返回多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return 2;
    }else{
        return 4;
    }
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        if (indexPath.row==0)
        {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"id1"];
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"id1"];
            cell.textLabel.text = @"我的位置";
            cell.imageView.image = [UIImage imageNamed:@"icon_map_pin_red"];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.textLabel.textColor = [UIColor colorWithRed:128 / 255.0 green:127 / 255.0 blue:125 / 255.0 alpha:1];
            return cell;
        }else
        {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"i1"];
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"i1"];
            cell.textLabel.text = @"清除缓存";
            cell.imageView.image = [UIImage imageNamed:@"icon_delete_40"];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            //设置字体颜色
            cell.textLabel.textColor = [UIColor colorWithRed:128 / 255.0 green:127 / 255.0 blue:125 / 255.0 alpha:1];
            
            return cell;
        }
    }else
    {
        if (indexPath.row==0) {
            
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"id2"];
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"id2"];
            cell.textLabel.text = @"我的锦囊";
            cell.imageView.image = [UIImage imageNamed:@"button_sticker_experience"];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.textLabel.textColor = [UIColor colorWithRed:128 / 255.0 green:127 / 255.0 blue:125 / 255.0 alpha:1];
            return cell;
            
        }else if (indexPath.row==1)
        {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"i3"];
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"i3"];
            cell.textLabel.text = @"关于小豌旅行";
            cell.imageView.image = [UIImage imageNamed:@"unconnected5"];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.textLabel.textColor = [UIColor colorWithRed:128 / 255.0 green:127 / 255.0 blue:125 / 255.0 alpha:1];
            return cell;
        }else if (indexPath.row==3)
        {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"i4"];
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"i4"];
            cell.imageView.image = [UIImage imageNamed:@"unconnected2"];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.textLabel.textColor = [UIColor colorWithRed:128 / 255.0 green:127 / 255.0 blue:125 / 255.0 alpha:1];
            cell.textLabel.text = @"联系我吧";
            return cell;
        }else{
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"i2"];
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"i2"];
            cell.textLabel.text = @"版本号";
            cell.imageView.image = [UIImage imageNamed:@"avatar_placeholder"];
            
            cell.textLabel.textColor = [UIColor colorWithRed:128 / 255.0 green:127 / 255.0 blue:125 / 255.0 alpha:1];
            UILabel *versionLabel = [UILabel new];
            versionLabel.text = @"小豌旅行 1.0";
            versionLabel.textColor = [UIColor colorWithRed:135 / 255.0 green:135 / 255.0 blue:135 / 255.0 alpha:1];
            [versionLabel sizeToFit];
            cell.accessoryView = versionLabel;
            return cell;
        }
    }
}

#pragma mark 监听选中哪行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        if (indexPath.row==0) {
            MapViewController *map = [[MapViewController alloc]init];
            [self presentViewController:map animated:YES completion:nil];
           // NSLog(@"位置");
        }
        else
        {
            [self clearCache];

        }
    }else{
        if (indexPath.row==0) {
          
            NSLog(@"锦囊");
            
        }else if (indexPath.row==1)
        {
            xiaowanViewController *xiaowan = [[xiaowanViewController alloc]init];
            [self presentViewController:xiaowan animated:YES completion:nil];
           // NSLog(@"旅行");
        }else if (indexPath.row==3){
            ConnectViewController *connect = [[ConnectViewController alloc]init];
            [self presentViewController:connect animated:YES completion:nil];
            
           // NSLog(@"联系");
        }
    }
}

- (void)clearCache//清除缓存每次没有变化
{
  
    //获取大小（判断）
    NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    NSUInteger pathSize = [self getCacheSizePath:caches];
    
    //手机上的换算单位是1000
    if (pathSize > 1000 *1000) {
        _str = [NSString stringWithFormat:@"缓存文件（%0.2fMB）",pathSize*1.0/1000/1000];
    }else if (pathSize > 1000){
       _str = [NSString stringWithFormat:@"缓存文件（%0.2fKB）",pathSize*1.0/1000];
    }else{
       _str = [NSString stringWithFormat:@"缓存文件（%luB）",pathSize];
    }
    
    
    //创建提示框
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"清除缓存" message:_str preferredStyle:UIAlertControllerStyleAlert];

    // 创建AlertAction
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
        [alertController dismissViewControllerAnimated:YES completion:nil];
    }];
    
    UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"清除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) {
        // 清除缓存，就是删除caches文件中的文件夹
        //建立文件管理者
        NSFileManager *manager = [NSFileManager defaultManager];
        //删除文件(找到路径)
        NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
        
        NSArray *array = [manager contentsOfDirectoryAtPath:cachesPath error:nil];
        for (NSString *path in array) {
            //拼接路径
            NSString *detailPath = [cachesPath stringByAppendingPathComponent:path];
            //删除
            [manager removeItemAtPath:detailPath error:nil];
        }
        [self.tableView reloadData];
        
        [KVNProgress showSuccessWithStatus:@"清除成功"];
    }];
    
    // 将UIAlertAction添加到UIAlertController中
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    
    // present显示
    [self presentViewController:alertController animated:YES completion:nil];
}

#pragma mark 删除缓存(把caches里的数据删除)
-(NSInteger)getCacheSizePath:(NSString *)path{
    
    //建立文件管理者（获取文件夹属性）
    NSFileManager *manager = [NSFileManager defaultManager];
    
    NSUInteger pathSize = 0;
    //获取defaultPath下所有子路径
    NSArray *arr = [manager subpathsAtPath:path];
    //遍历所有子路径
    for (NSString *path1 in arr) {
        //拼接所有路径
        NSString *detailPath = [path stringByAppendingPathComponent:path1];
        //求出每个文件的大小，实现累加
        pathSize += [manager attributesOfItemAtPath:detailPath error:nil].fileSize;
    }
    return pathSize;
    //NSLog(@"pathSize=%ld",pathSize);
    
}


@end
